#include "KMNetConnectedClient.h"

const unsigned char kDisconnectChannel = 0xFF;

KMNetConnectedClient::KMNetConnectedClient(ENetPeer *peer):mPeer(peer)
{
}

KMNetConnectedClient::~KMNetConnectedClient()
{
}

bool KMNetConnectedClient::isConnected()
{
    if(mPeer)
    {
        return (mPeer->state == ENET_PEER_STATE_CONNECTED);
    }
    return false;
}

int KMNetConnectedClient::getIP()
{
    int IP = 0;
    if(mPeer)
    {
        IP = mPeer->address.host;
    }
    return IP;
}

void KMNetConnectedClient::send(const KMNetMsgOut &msg,
    bool reliable,
    unsigned char channel)
{
    ENetPacket *packet;
    packet = enet_packet_create(msg.getData(),
                                msg.getLength(),
                                reliable ? ENET_PACKET_FLAG_RELIABLE : 0);
    if (mPeer && packet)
    {
        enet_peer_send(mPeer, channel, packet);
    }
}

void KMNetConnectedClient::disconnect(const KMNetMsgOut &msg)
{
    if (isConnected())
    {
        send(msg, ENET_PACKET_FLAG_RELIABLE, kDisconnectChannel);
        enet_peer_disconnect(mPeer, 0);
    }
}
